<template>
  <div class="standardFormula">
    <el-form label-suffix="：">
      <el-form-item label="扣款方式" v-if="form.type < 18">
        <el-select v-model="form.type">
          <el-option v-for="item in cutList" :key="item.id" :value="item.type" :label="item.typeName" />
        </el-select>
      </el-form-item>
      <template v-if="[1, 7].includes(form.type)">
        <el-form-item :label="label"> <el-input-number :precision="precision.money" :min="0" :controls="false" v-model="formFormula.otherFormula" />元/1分钟 * {{ name }}分钟数 </el-form-item>
        <el-form-item :label="label1">
          {{ name }}分钟数在 <el-input-number :precision="precision.minute" :min="0" :controls="false" v-model="form.setup1" />分钟以内,不扣款；
          <deduct-index v-model="form.isExempt" :name="name + '分钟数'" />
        </el-form-item>
      </template>

      <template v-else-if="[2, 8].includes(form.type)">
        <el-form-item :label="label"> <el-input-number :precision="precision.money" :min="0" :controls="false" v-model="formFormula.otherFormula" />元/一次 * {{ name }}次数 </el-form-item>
        <el-form-item :label="label1">
          {{ name }}次数在 <el-input-number :precision="precision.time" :min="0" :controls="false" v-model="form.setup1" />次以内,不扣款；
          <deduct-index v-model="form.isExempt" :name="name + '次数'" />
        </el-form-item>
      </template>

      <template v-else-if="[3, 9].includes(form.type)">
        <el-form-item :label="label"> <el-input-number :precision="precision.money" :min="0" :controls="false" v-model="formFormula.otherFormula" />元 </el-form-item>
      </template>

      <template v-else-if="[4, 10].includes(form.type)">
        <el-form-item :label="label"> <section-or-step :name="name" :type.sync="form.type" ref="sectionOrStep" /> </el-form-item>
        <el-form-item :label="label1">
          累计{{ name }}次数
          <el-input-number :precision="precision.time" :min="0" :controls="false" v-model="form.setup1" />
          次，且每次迟到分钟数在
          <el-input-number :precision="precision.minute" :min="0" :controls="false" v-model="form.setup2" />
          分钟以内，不扣款；
          <deduct-index v-model="form.isExempt" :name="'累计' + name + '次数'" />
        </el-form-item>
      </template>

      <template v-else-if="[5, 11].includes(form.type)">
        <el-form-item :label="label"> <section-or-step :name="name" :type.sync="form.type" ref="sectionOrStep" /> </el-form-item>
        <el-form-item :label="label1">
          累计{{ name }}分钟数在
          <el-input-number :precision="precision.minute" :min="0" :controls="false" v-model="form.setup1" />
          分钟以内，不扣款；
          <deduct-index v-model="form.isExempt" :name="'累计' + name + '分钟数'" />
        </el-form-item>
      </template>

      <template v-else-if="[6, 12].includes(form.type)">
        <el-form-item :label="label"> <section-or-step :name="name" :type.sync="form.type" unit="次" ref="sectionOrStep" /> </el-form-item>
        <el-form-item :label="label1">
          累计{{ name }}次数
          <el-input-number :precision="precision.time" :min="0" :controls="false" v-model="form.setup1" />
          次以内，不扣款；
          <deduct-index v-model="form.isExempt" :name="'累计' + name + '次数'" />
        </el-form-item>
      </template>

      <template v-else-if="form.type == 14">
        <el-form-item :label="label"> <el-input-number :precision="precision.money" :min="0" :controls="false" v-model="formFormula.otherFormula" />元/一次 * 缺卡次数</el-form-item>
        <el-form-item :label="label1">
          缺卡次数在 <el-input-number :precision="precision.time" :min="0" :controls="false" v-model="form.setup1" />次以内，不扣款；
          <deduct-index v-model="form.isExempt" name="缺卡次数" />
        </el-form-item>
      </template>

      <template v-else-if="form.type == 15">
        <el-form-item :label="label"> <section-or-step :type.sync="form.type" name="缺卡" unit="次" ref="sectionOrStep" /> </el-form-item>
        <el-form-item :label="label1">
          累计缺卡次数
          <el-input-number :precision="precision.time" :min="0" :controls="false" v-model="form.setup1" />
          次以内，不扣款；
          <deduct-index v-model="form.isExempt" name="累计缺卡次数" />
        </el-form-item>
      </template>

      <template v-else-if="form.type == 16">
        <el-form-item :label="label"> <el-input-number :precision="precision.money" :min="0" :controls="false" v-model="formFormula.otherFormula" />元/一次 * 旷工次数</el-form-item>
        <el-form-item :label="label1">
          旷工次数在 <el-input-number :precision="precision.time" :min="0" :controls="false" v-model="form.setup1" />次以内，不扣款；
          <deduct-index v-model="form.isExempt" name="旷工次数" />
        </el-form-item>
      </template>

      <template v-else-if="form.type == 17">
        <el-form-item label="扣款基数">
          <checkbox-group v-model="form.salaryBaseNames" :names="['基础工资']" />
        </el-form-item>
        <el-form-item label="扣款比例"><el-input-number :precision="precision.pro" :min="0" :controls="false" v-model="form.proportion" />%</el-form-item>
        <el-form-item :label="label">扣款金额=扣款基数/月计薪天数/每天工作时长*扣款比例*旷工时长</el-form-item>
      </template>

      <template v-else-if="form.type == 18">
        <el-form-item label="全勤奖金额"> <el-input-number :precision="precision.money" :min="0" v-model="formFormula.otherFormula" :controls="false" />元 </el-form-item>
        <span>以下任意一项满足，均扣除全勤奖</span>
        <el-form-item>
          <checkbox-group v-model="form.salaryBaseNames" :names="['请假扣款', '考勤扣款']" />
        </el-form-item>
      </template>

      <template v-else-if="[22, 23, 24, 25, 26, 27].includes(form.type)">
        <el-form-item label="计薪基数">
          <checkbox-group v-model="form.salaryBaseNames" :names="['基础工资']" />
        </el-form-item>
        <el-form-item label="计薪方式">
          <el-select v-model="form.type">
            <el-option v-for="item in cutList" :key="item.id" :value="item.type" :label="item.typeName" />
          </el-select>
        </el-form-item>
        <template v-if="[22, 24, 26].includes(form.type)">
          <el-form-item label="计薪天数标准">
            <el-select v-model="form.salaryType">
              <el-option v-for="item in salaryList" :key="item.id" :value="item.type" :label="item.typeName" />
            </el-select>
          </el-form-item>
          <el-form-item v-if="form.salaryType == 21" label="计薪天数"><el-input-number :precision="precision.day" :min="0" :controls="false" v-model="formFormula.otherFormula"/></el-form-item>
          <el-form-item label="计薪比例"><el-input-number :precision="precision.pro" :min="0" :controls="false" v-model="form.proportion" />%</el-form-item>
          <el-form-item :label="label">计薪金额=计薪基数/计薪标准*计薪比例*加班时长</el-form-item>
        </template>
        <template v-else-if="[23, 25, 27].includes(form.type)">
          <el-form-item>
            按工作日加班
            <el-select style="width:110px" v-model="formFormula.calculationType">
              <el-option v-for="item in types" :key="item.id" :value="item.id" :label="item.label" />
            </el-select>
            <el-input-number :precision="precision.money" :min="0" v-model="formFormula.otherFormula" :controls="false" />
            元/时
          </el-form-item>
        </template>
      </template>

      <template v-else-if="form.type == 28">
        <el-form-item label="扣款方式">
          <el-select v-model="form.type">
            <el-option v-for="item in cutList" :key="item.id" :value="item.type" :label="item.typeName" />
          </el-select>
        </el-form-item>
        <el-form-item label="扣除基数">
          <checkbox-group v-model="form.salaryBaseNames" :names="['基础工资']" />
        </el-form-item>
        <el-form-item label="扣除天数标准">
          <el-select v-model="form.salaryType">
            <el-option v-for="item in salaryList" :key="item.id" :value="item.type" :label="item.typeName" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.salaryType == 21" label="计薪天数"><el-input-number :precision="precision.day" :min="0" :controls="false" v-model="formFormula.otherFormula"/></el-form-item>
        <el-form-item label="计薪比例"><el-input-number :precision="precision.pro" :min="0" :controls="false" v-model="form.proportion" />%</el-form-item>
        <el-form-item :label="label">扣款金额=扣款基数/月计薪天数/每天工作时长*扣款比例*请假时长</el-form-item>
        <el-form-item :label="label1">
          每月累计
          <el-input-number :precision="precision.hour" :min="0" v-model="form.setup1" :controls="false" />
          小时，不扣款；
          <deduct-index v-model="form.isExempt" />
        </el-form-item>
      </template>
      <template v-else-if="form.type == 29">
        <el-form-item label="扣款方式">
          <el-select v-model="form.type">
            <el-option v-for="item in cutList" :key="item.id" :value="item.type" :label="item.typeName" />
          </el-select>
        </el-form-item>
      </template>
    </el-form>
  </div>
</template>

<script>
import store from "@/store";
import sectionOrStep from "./sectionOrStep.vue";
import checkboxGroup from "./checkboxGroup.vue";
import { getValueKey, names, types } from "./config";
import HandleSystem from "./handleSystem";
import { convertWord } from "@/util/xc/utils";
import DeductIndex from "./deductIndex.vue";
export default {
  name: "standardFormula",
  components: { sectionOrStep, checkboxGroup, DeductIndex },
  props: {
    item: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.label = "计算公式";
    this.label1 = "特殊设置";
    this.types = types.slice(0, 2);
    return {
      form: {
        type: "",
        salaryBaseNames: [],
        salaryType: 19,
        isExempt: true
      },
      formFormula: {
        calculationType: null,
        otherFormula: null
      },
      precision: {
        money: 2, // 金额
        minute: 2, // 分钟
        time: 0, // 次
        day: 0, // 天
        hour: 2, // 时
        pro: 0 // 比例
      }
    };
  },
  watch: {
    "form.type": {
      handler(val) {
        console.log(val);
      }
    }
  },
  computed: {
    name() {
      return names[this.item.namePy];
    },

    /** 类型列表 */
    cutList() {
      return store.getters.getTypes(this.item).filter(item => ![3, 9, 16].includes(item.type));
    },

    /** 天数标准列表 */
    salaryList() {
      const title = "tianshubiaozhun";
      return store.getters.getTypes({ namePy: title, titlePy: title });
    }
  },
  methods: {
    async setParams() {
      const { systemFormulaTypes, systemFormulas } = await this.getParams();
      this.setJointOwned(systemFormulaTypes);
      this.setJointOwned(systemFormulas);
      HandleSystem.setFormula(systemFormulaTypes, systemFormulas, this.item.namePy);
    },
    async getParams() {
      return new Promise(async (resolve, reject) => {
        const { type } = this.form;
        const params = this.$refs?.sectionOrStep?.getParams() || {};
        let systemFormulaTypes = [{ type }];
        let systemFormulas = [{}];
        const arr = getValueKey[type];
        const a1 = arr[0].every(k => {
          let str = this.form[k] === false ? false : this.form[k] || params[k];
          if (k == "ladderOrSubsection") str = params[k];
          if (this.notEmpty(str, k)) {
            if (k == "salaryBaseNames") str = str.join(",");
            else if (k == "proportion") str = Number(str) / 100;
            systemFormulaTypes[0][k] = str;
          } else return false;
          return true;
        });
        if (!a1) {
          reject();
          return;
        }
        const a2 = arr[1].every(k => {
          if (k == "steps") {
            if (this.notEmpty(this.$refs.sectionOrStep.verify(), "setp")) {
              systemFormulas = params[k];
            } else return false;
          } else if (k == "otherFormula" && [22, 24, 26, 28].includes(this.form.type)) {
            if (this.form.salaryType == 21) {
              if (this.notEmpty(this.formFormula[k], k)) systemFormulas[0][k] = this.formFormula[k];
              else return false;
            }
          } else {
            if (this.notEmpty(this.formFormula[k], k)) systemFormulas[0][k] = this.formFormula[k];
            else return false;
          }
          return true;
        });
        if (!a2) {
          reject();
          return;
        }

        const data = { systemFormulaTypes, systemFormulas };
        console.log(data);
        resolve(data);
      });
    },
    notEmpty(str, k) {
      console.group("验证参数", str, k, this.form);
      let bol = str?.bol != undefined ? str.bol : str;
      let message = "请完善系统公式";
      if (["setup1", "setup2", "isExempt"].includes(k)) bol = true;
      else if (Array.isArray(str)) bol = !!str.length;
      else if (typeof str == "object" && str?.message) message = str.message;
      console.log(bol);
      console.groupEnd();
      if (!bol) {
        this.$message({ message, type: "warning" });
        return false;
      }
      return true;
    },

    /** 设置共有参数, 并删除空数组 */
    setJointOwned(arr) {
      const len = arr.length;
      for (let i = 0; i < len; i++) {
        const o = arr[i];
        const { titlePy, namePy } = this.item;
        if (Object.keys(o).length) {
          o.itemPy = titlePy;
          o.columnPy = namePy;
        } else {
          arr.splice(i, 1);
        }
      }
    }
  },
  created() {
    this.item.titlePy = convertWord(this.item.title);
    console.log(this.item);
  },
  mounted() {
    const { namePy } = this.item;
    const { systemFormulaTypes = [], systemFormulas = [] } = HandleSystem.getFormula(namePy);
    this.form.type = this.cutList?.[0]?.type;
    // 进行数据返填
    this.form.type = systemFormulaTypes?.[0]?.type || this.form.type;
    const arr = getValueKey[this.form.type];
    if (!arr) return;
    arr[0].forEach(k => {
      let str = systemFormulaTypes?.[0]?.[k];
      if (str || str === false) {
        if (k == "salaryBaseNames") str = str.split(",");
        else if (k == "proportion") str = Number(str) * 100;
        this.form[k] = str;
      } else {
        if (k == "proportion") this.form[k] = 150;
      }
    });
    arr[1].forEach(k => {
      console.log(systemFormulaTypes?.[0], k);
      if (["steps", "ladderOrSubsection"].includes(k)) {
        this.$nextTick(() => {
          this.$refs.sectionOrStep.setData(systemFormulas, systemFormulaTypes?.[0].ladderOrSubsection);
        });
      }
      this.formFormula[k] = systemFormulas?.[0]?.[k];
    });
    console.log(this.formFormula, this.form);
  }
};
</script>
<style lang="less" scoped>
.standardFormula {
  margin-top: 20px;
  color: #606266;
  width: 80%;
  :deep(.el-form) {
    .el-form-item {
      display: flex;
      .el-form-item__content {
        display: inline-flex;
        flex: 1;
        white-space: nowrap;
        align-items: flex-end;
        flex-wrap: wrap;
        width: 850px;
        .el-input {
          width: auto;
          min-width: 50px;
        }
      }
    }
  }
}
</style>
